let fs = require("fs");


function uploadSinglefile(req, res) {
    if (req.file.length === 0) { //判断一下文件是否存在，也可以在前端代码中进行判断。
        res.render("error", {
            message: "上传文件不能为空！"
        });
        return
    } else {
        let file = req.file;
        fs.renameSync('./public/uploads/' + file.filename, './public/uploads/' + file.originalname); //这里修改文件名字，比较随意。
        // 设置响应类型及编码
        res.set({
            'content-type': 'application/json; charset=utf-8'
        });

        res.send({
            'code': 200,
            'msg': "上传成功！"
        });
    }
}

function uploadMultiFile(req, res) {

    let files = req.files
    for (let i in files) {
        fs.renameSync('./public/uploads/' + files[i].filename, './public/uploads/' + files[i].originalname)
    }

    // 设置响应类型及编码
    res.set({
        'content-type': 'application/json; charset=utf-8'
    });

    res.send({
        'code': 200,
        'msg': "上传成功！"
    });

}

module.exports = {
    uploadMultiFile,
    uploadSinglefile
}